package com.xbongbong.parent.util;

import com.alibaba.fastjson.JSON;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.log.constant.LogMemoConstant;
import com.xbongbong.paas.pojo.AttrValuePojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.parent.pojo.LogItemPoJo;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/11/17 15:17
 */
public class LogUtil {

    /**
     * 获取改变的日志信息
     * @param logFieldEditedList
     * @return
     * @throws XbbException
     */
    public static String getChangedLogItemsStr(List<AttrValuePojo> logFieldEditedList) throws XbbException {
        if (CollectionsUtil.isEmpty(logFieldEditedList)) {
            return "";
        }

        List<String> changedLogItemList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (AttrValuePojo av : logFieldEditedList) {
            //把字段“%s”的值改为“%s”
            String  item = String.format(I18nMessageUtil.getMessage(LogMemoConstant.LOG_MEMO_USER_UPDATE_FORM_DATA_ITEM),
                    av.getAttrName(), av.getValue());
            if (av.getValue() instanceof Map) {
                LogItemPoJo logItemPoJo = JSON.parseObject(JSON.toJSONString(av.getValue()), LogItemPoJo.class);
                if (Objects.nonNull(logItemPoJo.getText())) {
                    // 下拉框多选，复选框
                    item = String.format(I18nMessageUtil.getMessage(LogMemoConstant.LOG_MEMO_USER_UPDATE_FORM_DATA_ITEM),
                            av.getAttrName(), logItemPoJo.getText());
                } else if (Objects.nonNull(logItemPoJo.getName())) {
                    // 部门多选，用户多选
                    item = String.format(I18nMessageUtil.getMessage(LogMemoConstant.LOG_MEMO_USER_UPDATE_FORM_DATA_ITEM),
                            av.getAttrName(), logItemPoJo.getName());
                }
            } else if (av.getValue() instanceof List) {
                // 多选项，部门多选，用户多选，下拉框多选，复选框
                List<LogItemPoJo> list = new ArrayList<>();
                try {
                    list = JSON.parseArray(JSON.toJSONString(av.getValue()), LogItemPoJo.class);
                } catch (Exception e) {
                    list = new ArrayList<>();
                }
                List<String> valueList = new ArrayList<>();
                list.forEach(logItemPoJo->{
                    if (Objects.nonNull(logItemPoJo.getText())) {
                        // 下拉框多选，复选框
                        valueList.add(logItemPoJo.getText());
                    } else if (Objects.nonNull(logItemPoJo.getName())) {
                        // 部门多选，用户多选
                        valueList.add(logItemPoJo.getName());
                    }
                });
                item = String.format(I18nMessageUtil.getMessage(LogMemoConstant.LOG_MEMO_USER_UPDATE_FORM_DATA_ITEM),
                        av.getAttrName(), StringUtils.join(valueList, "、"));
            }

            changedLogItemList.add(item);
        }
        return StringUtils.join(changedLogItemList, "，");
    }
}
